package smartcrud.spring.models.rbac;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import smartcrud.common.orm.Page;
import smartcrud.common.orm.PropertyFilter;
import smartcrud.common.orm.jdbc.Sql;

@Repository
public class MenuDaoJdbcImpl implements MenuDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public Menu createMenu(final Menu o) {
		final String sql = "insert into RBAC_MENU(LEVEL,NAME,MENU_ORDER,URL,PID,COMMENT) values (?,?,?,?,?,?)";
		KeyHolder keyHolder = new GeneratedKeyHolder();
		jdbcTemplate.update(/* sql, */new PreparedStatementCreator() {
			@Override
			public PreparedStatement createPreparedStatement(Connection con)
					throws SQLException {
				PreparedStatement ps = con.prepareStatement(sql,
						Statement.RETURN_GENERATED_KEYS);
				int z = 0;
				ps.setInt(++z, o.getLevel());
				ps.setString(++z, o.getName());
				ps.setInt(++z, o.getMenuOrder());
				ps.setString(++z, o.getUrl());
				ps.setLong(++z, o.getPid());
				ps.setString(++z, o.getComment());
				return ps;
			}
		}, keyHolder);
		o.setId(keyHolder.getKey().longValue());
		return o;
	}

	@Override
	public void deleteMenu(Long id) {
		jdbcTemplate.update("delete from RBAC_ROLE_MENU where menu_id=?",id);
		jdbcTemplate.update("delete from RBAC_MENU where ID=?", id);
		
	}

	@Override
	public void updateMenu(Menu o) {
		final String sql = "update RBAC_MENU set LEVEL=?,NAME=?,MENU_ORDER=?,URL=?,PID=?,COMMENT=? where ID=?";
		jdbcTemplate.update(sql, 
				o.getLevel(), 
				o.getName(), 
				o.getMenuOrder(), 
				o.getUrl(), 
				o.getPid(), 
				o.getComment(), 
				o.getId() 
				);
	}
	@Override
	public void updateMenuOrder(List<Menu> menus) {
		List batchArgs = new ArrayList();
		if (menus != null) {
			for (Menu menu : menus) {
				Object[] args = new Object[] { menu.getMenuOrder(), menu.getId()};
				batchArgs.add(args);
			}
		}
		jdbcTemplate.batchUpdate("update RBAC_MENU set menu_order=? where id=?",batchArgs);
	}

	@Override
	public Menu findMenuById(Long id) {
		final String sql = "select ID,LEVEL,NAME,MENU_ORDER,URL,PID,COMMENT from RBAC_MENU where ID=?";
		return jdbcTemplate.queryForObject(sql, new RowMapper<Menu>() {
			@Override
			public Menu mapRow(ResultSet rs, int rowNum) throws SQLException {
				Menu o = new Menu();
				o.setId(rs.getLong("ID"));
				o.setLevel(rs.getInt("LEVEL"));
				o.setName(rs.getString("NAME"));
				o.setMenuOrder(rs.getInt("MENU_ORDER"));
				o.setUrl(rs.getString("URL"));
				o.setPid(rs.getLong("PID"));
				o.setComment(rs.getString("COMMENT"));
				return o;
			}
		}, id);
	}

	@Override
	public List<Menu> findAllMenus() {
		final String sql = "select ID,LEVEL,NAME,MENU_ORDER,URL,PID,COMMENT from RBAC_MENU where id!=0 order by menu_order asc";
		return jdbcTemplate.query(sql, new RowMapper<Menu>() {
			@Override
			public Menu mapRow(ResultSet rs, int rowNum) throws SQLException {
				Menu o = new Menu();
				o.setId(rs.getLong("ID"));
				o.setLevel(rs.getInt("LEVEL"));
				o.setName(rs.getString("NAME"));
				o.setMenuOrder(rs.getInt("MENU_ORDER"));
				o.setUrl(rs.getString("URL"));
				o.setPid(rs.getLong("PID"));
				o.setComment(rs.getString("COMMENT"));
				return o;
			}
		});
	}
	
	public int count() {
		return jdbcTemplate.queryForInt("select count(*) from RBAC_MENU where id!=0");
	}
	
	@Override
	public int count(Page<Menu> page,final List<PropertyFilter> filters) {
		Sql sql = new Sql("select count(*) from RBAC_MENU where 1=1 ", page,filters);
		sql.build4count();
		return jdbcTemplate.queryForInt(sql.getSqlBody(), sql.getSqlParams());
	}

	@Override
	public Page<Menu> findByPage(Page<Menu> page, final List<PropertyFilter> filters) {
		if (page.isAutoCount()) {
			int totalCount = count(page, filters);
			page.setTotalCount(totalCount);
		}
		
		Sql sql = new Sql("select * from RBAC_MENU where id!=0 ", page, filters);
		sql.build();

		List<Menu> content = jdbcTemplate.query(sql.getSqlBody(),
				new RowMapper<Menu>() {
					@Override
					public Menu mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Menu o = new Menu();
						o.setId(rs.getLong("ID"));
						o.setLevel(rs.getInt("LEVEL"));
						o.setName(rs.getString("NAME"));
						o.setMenuOrder(rs.getInt("MENU_ORDER"));
						o.setUrl(rs.getString("URL"));
						o.setPid(rs.getLong("PID"));
						o.setComment(rs.getString("COMMENT"));
						return o;
					}
				}, sql.getSqlParams());

		page.setContent(content);
		return page;
	}

	@Override
	public List<Menu> findTopLevelMenus() {
		final String sql = "select ID,LEVEL,NAME,MENU_ORDER,URL,PID,COMMENT from RBAC_MENU t where t.level=1 order by menu_order asc";
		return jdbcTemplate.query(sql, new RowMapper<Menu>() {
			@Override
			public Menu mapRow(ResultSet rs, int rowNum) throws SQLException {
				Menu o = new Menu();
				o.setId(rs.getLong("ID"));
				o.setLevel(rs.getInt("LEVEL"));
				o.setName(rs.getString("NAME"));
				o.setMenuOrder(rs.getInt("MENU_ORDER"));
				o.setUrl(rs.getString("URL"));
				o.setPid(rs.getLong("PID"));
				o.setComment(rs.getString("COMMENT"));
				return o;
			}
		});
	}

}
